package com.GlobalPaint.app.ui.Mine;

import android.Manifest;
import android.graphics.Bitmap;
import android.os.Build;
import android.os.Bundle;
import android.support.v4.app.ActivityCompat;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.GlobalPaint.app.R;
import com.GlobalPaint.app.bean.ReturnStatusResultEntity;
import com.GlobalPaint.app.bean.UpdateTO;
import com.GlobalPaint.app.ui.App;
import com.GlobalPaint.app.utils.BitmapUtil;
import com.GlobalPaint.app.utils.CommUtility;
import com.GlobalPaint.app.utils.Constants;
import com.GlobalPaint.app.utils.DataManager;
import com.squareup.picasso.MemoryPolicy;
import com.squareup.picasso.NetworkPolicy;
import com.squareup.picasso.Picasso;
import com.umeng.socialize.ShareAction;
import com.umeng.socialize.UMShareListener;
import com.umeng.socialize.bean.SHARE_MEDIA;
import com.umeng.socialize.media.UMImage;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class ShareActivity extends AppCompatActivity {
    @BindView(R.id.iv_goback)
    ImageView ivGoback;
    @BindView(R.id.tv_common_center)
    TextView tvCommonCenter;
    @BindView(R.id.tv_fresh)
    TextView tvFresh;
    @BindView(R.id.rl_ios)
    RelativeLayout rlIos;
    @BindView(R.id.imageandorid)
    ImageView imageandorid;
    @BindView(R.id.rl_andorid)
    RelativeLayout rlAndorid;
    @BindView(R.id.image)
    ImageView image;
    @BindView(R.id.ll_title)
    LinearLayout llTitle;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_share);
        ButterKnife.bind(this);
        initView();
        //android();
        ios();
        Picasso.with(App.context).load(Constants.JASON_SERVICE_URLSJ + "CommonFunction/app/appupdate/qrCode?type=android&roleid=1&msize=550")
                .memoryPolicy(MemoryPolicy.NO_CACHE)

                .networkPolicy(NetworkPolicy.NO_CACHE)
                .into(imageandorid);
    }

    private void ios() {
        String url = "https://itunes.apple.com/app/id1359177156";
        try {
            Bitmap bitmap = BitmapUtil.createQRCode(url, 550);
            image.setImageBitmap(bitmap);
        } catch (Exception ex) {
        }
    }

    private void android() {

        String urlString = Constants.JASON_SERVICE_URLSJ + "CommonFunction/app/appupdate/qrCode?type=android&roleid=1&msize=550";
        DataManager.getInst().getHttpRequestJsonClass(urlString, Constants.key, UpdateTO.class, new DataManager.Callback() {
            private UpdateTO updateTO;

            @Override
            public void onSuccess(Object o) {
                updateTO = (UpdateTO) o;
                try {
                    Bitmap bitmap = BitmapUtil.createQRCode(updateTO.getData().get(0).getUpdateurl(), 550);
                    imageandorid.setImageBitmap(bitmap);
                } catch (Exception ex) {
                }
            }

            @Override
            public void onFailure(ReturnStatusResultEntity error) {
                Toast.makeText(ShareActivity.this, "下载失败", Toast.LENGTH_LONG).show();
            }
        });
    }

    private void initView() {
        tvCommonCenter.setText("下载");
        tvFresh.setVisibility(View.VISIBLE);
        tvFresh.setText("分享");
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
    }

    @OnClick({R.id.iv_goback, R.id.tv_fresh, R.id.rl_ios, R.id.rl_andorid})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.iv_goback:
                finish();
                break;
            case R.id.tv_fresh:
               /* Bitmap bmp = CommUtility.getImage(ShareActivity.this, llTitle);
                UMImage image = new UMImage(ShareActivity.this.getApplicationContext(), bmp);
                image.compressStyle = UMImage.CompressStyle.SCALE;
                new ShareAction(ShareActivity.this)
                        .withMedia(image)
                        .withText("hello")
                        .setDisplayList(SHARE_MEDIA.WEIXIN_CIRCLE, SHARE_MEDIA.WEIXIN)
                        .setCallback(umShareListener).open();*/


                break;
            case R.id.rl_ios:
                if(Build.VERSION.SDK_INT>=23){
                    String[] mPermissionList = new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE,Manifest.permission.ACCESS_FINE_LOCATION,Manifest.permission.CALL_PHONE,Manifest.permission.READ_LOGS,Manifest.permission.READ_PHONE_STATE, Manifest.permission.READ_EXTERNAL_STORAGE,Manifest.permission.SET_DEBUG_APP,Manifest.permission.SYSTEM_ALERT_WINDOW,Manifest.permission.GET_ACCOUNTS,Manifest.permission.WRITE_APN_SETTINGS};
                    ActivityCompat.requestPermissions(this,mPermissionList,123);
                }
                Bitmap bmp1 = CommUtility.getImage(ShareActivity.this, rlIos);
                UMImage image1 = new UMImage(ShareActivity.this.getApplicationContext(), bmp1);
                image1.compressStyle = UMImage.CompressStyle.SCALE;
                new ShareAction(ShareActivity.this)
                        .withMedia(image1)
                        .withText("hello")
                        .setDisplayList(SHARE_MEDIA.WEIXIN_CIRCLE, SHARE_MEDIA.WEIXIN)
                        .setCallback(umShareListener).open();
                break;
            case R.id.rl_andorid:
                Bitmap bmp2 = CommUtility.getImage(ShareActivity.this, rlAndorid);
                UMImage image2 = new UMImage(ShareActivity.this.getApplicationContext(), bmp2);
                image2.compressStyle = UMImage.CompressStyle.SCALE;
                new ShareAction(ShareActivity.this)
                        .withMedia(image2)
                        .withText("hello")
                        .setDisplayList(SHARE_MEDIA.WEIXIN_CIRCLE, SHARE_MEDIA.WEIXIN)
                        .setCallback(umShareListener).open();
                break;
        }
    }

    //分享回调
    private UMShareListener umShareListener = new UMShareListener() {
        @Override
        public void onStart(SHARE_MEDIA platform) {

            //分享开始的回调
        }

        @Override
        public void onResult(SHARE_MEDIA platform) {


            Toast.makeText(ShareActivity.this, platform + " 分享成功啦", Toast.LENGTH_SHORT).show();

        }

        @Override
        public void onError(SHARE_MEDIA platform, Throwable t) {
            Toast.makeText(ShareActivity.this, platform + " 分享失败啦", Toast.LENGTH_SHORT).show();
            if (t != null) {

            }
        }

        @Override
        public void onCancel(SHARE_MEDIA platform) {
            Toast.makeText(ShareActivity.this, platform + " 分享取消了", Toast.LENGTH_SHORT).show();
        }
    };

    @Override
    public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults) {


    }
}

